package com.lt.flashcard.model.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * A role in system. Each role will have mutiple permissions
 * @author ocsen
 * @version 1.0
 * @created 15-Aug-2012 17:03:42
 */
@Entity
@Table(name = "role", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Role implements java.io.Serializable {
	
	private static final long serialVersionUID = 1L;
	public static final String ROLE_ADMIN = "admin";
	public static final String ROLE_MOD = "mod";
	public static final String ROLE_AUTHENTICATED = "authenticated";
	public static final String ROLE_ANONYMOUS = "anonymous";
	
	private Integer id;
	private String name;
	private String description;
	private Set<Permission> permissions = new HashSet<Permission>(0);
	private Set<User> users = new HashSet<User>(0);

	public Role() {
	}

	public Role(String name, String description) {
		this.name = name;
		this.description = description;
	}

	public Role(String name, String description, Set<Permission> permissions,
			Set<User> users) {
		this.name = name;
		this.description = description;
		this.permissions = permissions;
		this.users = users;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name", unique = true, nullable = false, length = 100)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", nullable = false, length = 45)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "role_has_permission", joinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "permission_id", nullable = false, updatable = false) })
	public Set<Permission> getPermissions() {
		return this.permissions;
	}

	public void setPermissions(Set<Permission> permissions) {
		this.permissions = permissions;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_has_role", joinColumns = { @JoinColumn(name = "role_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) })
	public Set<User> getUsers() {
		return this.users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}
}